﻿using System;
using System.Data;
using System.Collections.Generic;
using Chenxin.Lib.Hr.Entity;
using System.Text;
namespace Chenxin.Lib.Hr.BLL
{
    /// <summary>
    /// 业务逻辑类Hr_DepartmentInfo 的摘要说明。
    /// </summary>
    public class Hr_DepartmentInfo
    {
        public Hr_DepartmentInfo()
        { }
        #region  成员方法

        //Cache服务类
        private Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.HR);

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo model)
        {
            string strKeyAllDeparts = Chenxin.Lib.Cache.CacheDataType.HR_ALLDEPARTMENTS;
            cs.Delete(strKeyAllDeparts);
            Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo model)
        {
            string strKeyAllDeparts = Chenxin.Lib.Cache.CacheDataType.HR_ALLDEPARTMENTS;
            cs.Delete(strKeyAllDeparts);
            Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(Guid DepartId)
        {
            string strKeyAllDeparts = Chenxin.Lib.Cache.CacheDataType.HR_ALLDEPARTMENTS;
            cs.Delete(strKeyAllDeparts);
            Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.Delete(DepartId);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo GetModel(Guid DepartId)
        {

            return Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(DepartId);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetList(strWhere);
        }

        /// <summary>
        /// 获得城市ID
        /// </summary>
        public string GetCityId(string strWhere)
        {
            string cityid = "";
            DataSet ds = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetList(strWhere);
            if (ds != null)
                cityid = ds.Tables[0].Rows[0]["CityId"].ToString();
            return cityid;
        }

        ///// <summary>
        ///// 获得前几行数据
        ///// </summary>
        //public DataSet GetList(int Top, string strWhere, string filedOrder)
        //{
        //    return Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetList(Top, strWhere, filedOrder);
        //}
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo> GetModelList(string strWhere)
        {
            DataSet ds = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }

        public List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo> GetAllDeparts()
        {
            DataSet ds;
            string strKeyAllDeparts = Chenxin.Lib.Cache.CacheDataType.HR_ALLDEPARTMENTS;
            ds = cs.GetDataSet(strKeyAllDeparts);
            if (!cs.Hit)
            {
                ds = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetList("");
                cs.Set(strKeyAllDeparts, ds);
            }
            
            return DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo> DataTableToList(DataTable dt)
        {
            List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo> modelList = new List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo();
                    if (dt.Rows[n]["DepartId"].ToString() != "")
                    {
                        model.DepartId = new Guid(dt.Rows[n]["DepartId"].ToString());
                    }
                    model.DepartCode = dt.Rows[n]["DepartCode"].ToString();
                    model.DepartName = dt.Rows[n]["DepartName"].ToString();
                    if (dt.Rows[n]["CityId"].ToString() != "")
                    {
                        model.CityId = new Guid(dt.Rows[n]["CityId"].ToString());
                    }
                    model.City = dt.Rows[n]["City"].ToString();
                    if (dt.Rows[n]["CompanyId"].ToString() != "")
                    {
                        model.CompanyId = new Guid(dt.Rows[n]["CompanyId"].ToString());
                    }
                    model.City = dt.Rows[n]["CompanyName"].ToString();
                    if (dt.Rows[n]["DepartKind"].ToString() != "")
                    {
                        model.DepartKind = dt.Rows[n]["DepartKind"].ToString();
                    }
                    if (dt.Rows[n]["DepartProperty"].ToString() != "")
                    {
                        model.DepartProperty = int.Parse(dt.Rows[n]["DepartProperty"].ToString());
                    }
                    model.Tel = dt.Rows[n]["Tel"].ToString();
                    if (dt.Rows[n]["IsOpen"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsOpen"].ToString() == "1") || (dt.Rows[n]["IsOpen"].ToString().ToLower() == "true"))
                        {
                            model.IsOpen = true;
                        }
                        else
                        {
                            model.IsOpen = false;
                        }
                    }
                    if (dt.Rows[n]["IsCalculate"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsCalculate"].ToString() == "1") || (dt.Rows[n]["IsCalculate"].ToString().ToLower() == "true"))
                        {
                            model.IsCalculate = true;
                        }
                        else
                        {
                            model.IsCalculate = false;
                        }
                    }
                    model.Email = dt.Rows[n]["Email"].ToString();
                    model.Address = dt.Rows[n]["Address"].ToString();
                    model.Region = dt.Rows[n]["Region"].ToString();
                    model.RegionCtr = dt.Rows[n]["RegionCtr"].ToString();
                    if (dt.Rows[n]["IsContractCenter"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsContractCenter"].ToString() == "1") || (dt.Rows[n]["IsContractCenter"].ToString().ToLower() == "true"))
                        {
                            model.IsContractCenter = true;
                        }
                        else
                        {
                            model.IsContractCenter = false;
                        }
                    }
                    if (dt.Rows[n]["AddTime"].ToString() != "")
                    {
                        model.AddTime = DateTime.Parse(dt.Rows[n]["AddTime"].ToString());
                    }
                    model.AddPerson = dt.Rows[n]["AddPerson"].ToString();
                    model.DepartSrc = dt.Rows[n]["DepartSrc"].ToString();
                    if (dt.Rows[n]["IsRoof"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsRoof"].ToString() == "1") || (dt.Rows[n]["IsRoof"].ToString().ToLower() == "true"))
                        {
                            model.IsRoof = true;
                        }
                        else
                        {
                            model.IsRoof = false;
                        }
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}


        #endregion  成员方法
    }
}

